#!/bin/bash

JAR_NAME=$(ls ./*.jar 2>/dev/null | grep 'code-knowledge-sample' | head -n 1)
if [ ! -f "$JAR_NAME" ]; then
  echo "[ERROR] 未找到 code-knowledge-sample jar 包，请确认 jar 文件在当前目录。"
  exit 1
fi

PIDS=$(ps aux | grep java | grep "$JAR_NAME" | grep -v grep | awk '{print $2}')
if [ -z "$PIDS" ]; then
  echo "未找到正在运行的 $JAR_NAME 进程。"
  exit 0
fi

echo "停止进程: $PIDS"
kill $PIDS

sleep 2

# 检查是否已停止
PIDS_CHECK=$(ps aux | grep java | grep "$JAR_NAME" | grep -v grep | awk '{print $2}')
if [ -z "$PIDS_CHECK" ]; then
  echo "服务已停止。"
else
  echo "部分进程未能停止，尝试强制 kill..."
  kill -9 $PIDS_CHECK
  echo "已强制停止。"
fi 